package com.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.dto.TplHtmlDto;
import com.oa.common.enums.CodeStatus;
import com.oa.common.enums.apps.FromFileType;
import com.oa.common.enums.apps.InputType;
import com.oa.entities.flows.Form;
import com.oa.service.FormService;
import com.oa.service.JedisService;
import com.oa.service.RedisService;
import com.oa.service.TplHtmlService;
import com.oa.untils.TplUntils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class TplHtmlServiceImpl implements TplHtmlService {

    @Autowired
    private JedisService redisService;

    private static final String TPL_HTML_VIEW="toa_tpl_";

    private static final String TPL_CONTENT_NAME="content";

    private static final String TPL_TEMPLATE_NAME="template";

    private static final String TPL_VIEW_NAME="view";

    private static final Logger logger= LoggerFactory.getLogger(TplHtmlServiceImpl.class);

    @Autowired
    private FormService formService;

    @Override
    public ResponseBack<String> submit(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        TplHtmlDto dto= JSONObject.parseObject(params,TplHtmlDto.class);
        if (dto==null){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }

        List<Form> forms= formService.findForm(dto.getTypeId());
        String contents= dto.getContent();
        String views= dto.getContent();
        String template= dto.getContent();
        String add=null;
        for (Form form: forms){
            if (form.getInputType()==InputType.NORMAL.getCode()){
                if (form.getFileType()== FromFileType.CANVAS.getCode()){
                    String regx="<\\s*input\\s+[^>]*?name\\s*=\""+form.getInputName()+"(.*?)\\1[^>]*?\\/s*>";
                    Pattern pattern= Pattern.compile(regx);
                    Matcher matcher= pattern.matcher(contents);
                    while (matcher.find()){
                        add= matcher.group();
                        logger.info("输出的模板"+add);
                        contents= contents.replace(add,TplUntils.appButton(form.getInputName(),false,null));
                        views= views.replace(add,"<!--{"+form.getInputName()+"}-->");
                    }
                    add= null;
                }
                if(form.getFileType()==FromFileType.TEXTAREA.getCode()){
                    Pattern pattern= Pattern.compile("<\\s*textarea\\s+[^>]*?name\\s*=\""+form.getInputName()+"\"(.*?)\\1[^>]*?>(.*?)\\1[^>]*?</textarea>");
                    Matcher matcher= pattern.matcher(contents);
                    while (matcher.find()){
                        add=matcher.group();
                        logger.warn("输出的结果:"+add);
                        contents= contents.replace(add,"<!--"+form.getInputName()+"s-->"
                                +TplUntils.appsTextarea(form.getInputName(),new Long(form.getW()).intValue(),
                                new Long(form.getH()).intValue(),form.getInputValue(),false)+"<!--"+form.getInputName()+"e-->");
                        views=views.replace(add,"<!--{"+form.getInputName()+"}-->");
                    }
                }
                if (form.getFileType()==FromFileType.CHECKBOX.getCode() || form.getFileType()==FromFileType.RADIO.getCode()){
                    Pattern pattern= Pattern.compile("<span\\s+[^>]*?title\\s*=\""+form.getInputName()+"\".*?>(.*?)</span>");
                    Matcher matcher=pattern.matcher(contents);
                    while (matcher.find()){
                        add= matcher.group();
                        if (form.getFileType()==FromFileType.CHECKBOX.getCode()){
                            contents= contents.replace(add,"<!--"+form.getInputName()+"s-->"
                                    +TplUntils.appsCheckbox(form.getName(),form.getOptiones(),form.getInputValue(),false)
                                    +"<!--"+form.getInputName()+"e-->");
                            views=views.replace(add,"<!--{"+form.getInputName()+"}-->");
                        }else {
                            contents= contents.replace(add,"<!--"+form.getInputName()+"s-->"
                                    +TplUntils.appsRadio(form.getName(),form.getOptiones(),form.getInputValue(),false)
                                    +"<!--"+form.getInputName()+"e-->");
                            views=views.replace(add,"<!--{"+form.getInputName()+"}-->");
                        }
                    }
                }
                if (form.getFileType()==FromFileType.SELECT.getCode()){
                    Pattern pattern= Pattern.compile("<select\\s+[^>]*?name\\s*=\""+form.getInputName()+"\".*?>(.*?)</select>");
                    Matcher matcher= pattern.matcher(contents);
                    while (matcher.find()){
                        add= matcher.group();
                        contents= contents.replace(add,"<!--"+form.getInputName()+"s-->"
                                +TplUntils.appsSelect(form.getName(),form.getOptiones(),form.getInputValue(),
                                new Long(form.getW()).intValue(),new Long(form.getH()).intValue(),false)
                                +"<!--"+form.getInputName()+"e-->");
                        views=views.replace(add,"<!--{"+form.getInputName()+"}-->");
                    }
                }else {
                    Pattern pattern= Pattern.compile("<\\s*input\\s+[^>]*?name\\s*=\""+form.getInputName()+"(.*?)\\1[^>]*?\\/s*>");
                    Matcher matcher= pattern.matcher(contents);
                    while (matcher.find()){
                         add= matcher.group();
                         contents= contents.replace(add,"<!--"+form.getInputName()+"s-->"
                                 +TplUntils.appsInput(form.getName(),
                                 new Long(form.getW()).intValue(),new Long(form.getH()).intValue(),form.getInputValue(),false)
                                 +"<!--"+form.getInputName()+"e-->");
                        views=views.replace(add,"<!--{"+form.getInputName()+"}-->");
                    }
                }
            }
        }
        String key= TPL_HTML_VIEW+dto.getTypeId();
        redisService.hset(key,TPL_CONTENT_NAME,contents);
        redisService.hset(key,TPL_VIEW_NAME,views);
        redisService.hset(key,TPL_TEMPLATE_NAME,template);
        return ResponseBack.success(null);
    }

    @Override
    public String getTpl(Long typeId) {
        String key= TPL_HTML_VIEW+typeId;
        return redisService.hget(key,TPL_TEMPLATE_NAME);
    }

    @Override
    public String getContent(Long type) {
        String key= TPL_HTML_VIEW+type;
        return redisService.hget(key,TPL_CONTENT_NAME);
    }

    @Override
    public String getViews(Long typeId) {
        String key= TPL_HTML_VIEW+typeId;
        return redisService.hget(key,TPL_VIEW_NAME);
    }
}
